//
//  GTDeleteCellView.m
//  WKWebViewStudy
//
//  Created by Jason Len on 2019/9/12.
//  Copyright © 2019年 Jason. All rights reserved.
//

#import "GTDeleteCellView.h"

@interface GTDeleteCellView ()

@property(nonatomic,strong,readwrite) UIView *backgroundView;
@property(nonatomic,strong,readwrite) UIButton *deleteButton;
@property(nonatomic,copy,readwrite) dispatch_block_t deleteBlock;

@end

@implementation GTDeleteCellView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:({
            self.backgroundView = [[UIView alloc]initWithFrame:self.bounds];
            self.backgroundView.backgroundColor = [UIColor blackColor];
            self.backgroundView.alpha =0.6;
            
            [self.backgroundView addGestureRecognizer:({
                UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(releaseDeleteView)];
                gesture;
            })];
            
            self.backgroundView;
        })];
        [self addSubview:({
            self.deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
            self.deleteButton.backgroundColor = [UIColor blueColor];
            [self.deleteButton addTarget:self action:@selector(_clickButton) forControlEvents:UIControlEventTouchUpInside];
            self.deleteButton;
        })];
    }
    return self;
}
//实现从左上角位置开始的动画
-(void) showDeleteView{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    //实现简单的0到1秒 deleteView位置重0到中间，大小从0到200动画
//    [UIView animateWithDuration:1.0f animations:^{
//        self.deleteButton.frame = CGRectMake((self.bounds.size.width-200)/2, (self.bounds.size.height-200)/2, 200, 200);
//    }];
    
    //实现稍复杂的从具有回弹效果的动画
    [UIView animateWithDuration:1.0f delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.deleteButton.frame = CGRectMake((self.bounds.size.width-200)/2, (self.bounds.size.height-200)/2, 200, 200);
    } completion:^(BOOL finished){
        NSLog(@"");
    }];
}

//实现从任意位置开始的动画
-(void) showDeleteViewFromPoint:(CGPoint)point clickBlock:(dispatch_block_t) clickBlock{
    
    _deleteButton.frame = CGRectMake(point.x, point.y, 0, 0);
    _deleteBlock = [clickBlock copy];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    //实现简单的0到1秒 deleteView位置重0到中间，大小从0到200动画
    //    [UIView animateWithDuration:1.0f animations:^{
    //        self.deleteButton.frame = CGRectMake((self.bounds.size.width-200)/2, (self.bounds.size.height-200)/2, 200, 200);
    //    }];
    
    //实现稍复杂的从具有回弹效果的动画
    [UIView animateWithDuration:1.0f delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.deleteButton.frame = CGRectMake((self.bounds.size.width-200)/2, (self.bounds.size.height-200)/2, 200, 200);
    } completion:^(BOOL finished){
//        NSLog(@"");
    }];
    
}

-(void) releaseDeleteView{
    [self removeFromSuperview];
}

-(void) _clickButton{
    if(_deleteBlock){
        _deleteBlock();
    }
    [self removeFromSuperview];
}
@end
